package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishServiceImpl dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("接受的新增菜品的信息为：{}", dishDTO);
        dishService.save(dishDTO);
//        redis清理缓存
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 根据id查看菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看菜品")
    public Result findById(@PathVariable Long id) {
        log.info("查看菜品的id为:{}", id);
        DishVO DishVOById = dishService.findById(id);
        return Result.success(DishVOById);
    }

    /**
     * 根据分类id获取菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id获取菜品")
    public Result findByCategoryId(@RequestParam long categoryId) {
        log.info("查看菜品的分类id为:{}", categoryId);
        List<Dish> dishByCategoryId = dishService.findByCategoryId(categoryId);
        return Result.success(dishByCategoryId);
    }

    /**
     * 菜品分类查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result findPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品表的信息为:{}", dishPageQueryDTO);
        PageResult page = dishService.findPage(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 商品起售/停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("商品起售/停售")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("当前商品状态:{}", status);
        dishService.startOrStop(status, id);
        cleanCache("dish_");
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result modifyDish(@RequestBody DishDTO dishDTO) {
        log.info("修改后商品的信息为:{]", dishDTO);
        dishService.modifyDish(dishDTO);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
//    SpringMVC自动将前端传来的String ids转换为 List<Long> ids
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("批量删除菜品的ids:{}", ids);
        dishService.deleteBatch(ids);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
